package com.sinoatma.physignsmonitor.fragment

import android.content.Intent
import android.widget.Toast
import com.common.frame.base.BaseFragment
import com.common.frame.extension.setSingleClick

import com.sinoatma.physignsmonitor.R
import com.sinoatma.physignsmonitor.constant.*
import com.sinoatma.physignsmonitor.databinding.FragmentSystemSettingBinding


/**
 * 系统设置
 */
class SystemSettingFragment : BaseFragment<SystemSettingViewModel,FragmentSystemSettingBinding>() {
    override fun layoutId(): Int =R.layout.fragment_system_setting

    override fun initData() {

    }

    override fun initView() {

    }

    override fun onResume() {
        super.onResume()
        if (baseIp.isNotEmpty()){
            binding.etServerAddress.setText(baseIp)
        }
        if (devicesNumber.isNotEmpty()){
            binding.etDevicesNumber.setText(devicesNumber)
        }
        if (ModeFlag==0){
            binding.raOfflineMode.isChecked=true
        }else{
            binding.raNetworkingMode.isChecked=true
        }
        if (BloodTransfusionModeFlag==0){
            binding.raBloodTransfusionMode.isChecked=true
        }else{
            binding.raDonateBloodMode.isChecked=true
        }

        if (BloodTypeModeFlag==0){
            binding.raBloodType1.isChecked=true
        }else{
            binding.raBloodType2.isChecked=true
        }

    }

    override fun initListener() {
        super.initListener()
        binding.tvSaveDevicesNumber.setSingleClick {
            if (binding.etDevicesNumber.text.toString().isNotEmpty()){
                devicesNumber=binding.etDevicesNumber.text.toString().trim()
                Toast.makeText(requireContext(),"保存成功",Toast.LENGTH_LONG).show()
            }else{
                Toast.makeText(requireContext(),"请输入设备编号在点击保存",Toast.LENGTH_LONG).show()
            }
        }

        binding.tvSaveServerAddress.setSingleClick {
            if (binding.etServerAddress.text.toString().isNotEmpty()){
                baseIp=binding.etServerAddress.text.toString().trim()
                Toast.makeText(requireContext(),"保存成功",Toast.LENGTH_LONG).show()
            }else{
                Toast.makeText(requireContext(),"请输入服务器在点击保存",Toast.LENGTH_LONG).show()
            }
        }

        binding.tvSaveMode.setSingleClick {
            if (binding.raNetworkingMode.isChecked){
                ModeFlag=1
            }
            else
            if (binding.raOfflineMode.isChecked){
                ModeFlag=0
            }

        }

        binding.tvSaveTransDonateMode.setSingleClick {
            if (binding.raBloodTransfusionMode.isChecked){
                BloodTransfusionModeFlag=0
            } else if (binding.raDonateBloodMode.isChecked){
                BloodTransfusionModeFlag=1
            }
            restartAppCompletely()
        }
        binding.tvSaveBlood.setSingleClick {
            if (binding.raBloodType1.isChecked){
                BloodTypeModeFlag=0
            }else if (binding.raBloodType2.isChecked){
                BloodTypeModeFlag=1
            }
        }



    }

     fun restartAppCompletely() {
        val packageManager = requireActivity().packageManager
        val intent = packageManager.getLaunchIntentForPackage(requireActivity().packageName)
        val componentName = intent?.component
        val mainIntent = Intent.makeRestartActivityTask(componentName)
        startActivity(mainIntent)
        Runtime.getRuntime().exit(0)
    }


}